package April.森林中的兔子;
//4月4日
import java.util.HashMap;
import java.util.Map;

class Solution {
    public int numRabbits(int[] answers) {
        int res =0;
        Map<Integer,Integer> map = new HashMap<>();
        for (int x=0;x<answers.length;x++){
            if (answers[x]==0){
                res++;
                continue;
            }
            if (!map.containsKey(answers[x])){
                map.put(answers[x],answers[x]);
                res+=answers[x];
                res++;
            }else {
                if (map.get(answers[x])>0){
                    map.put(answers[x],map.get(answers[x])-1);
                }else {
                    map.put(answers[x],answers[x]);
                    res+=answers[x];
                    res++;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int a[]={1,0,1,0,0};
        System.out.println(solution.numRabbits(a));
    }
}
/**
 *
 *
 *
 * public int numRabbits(int[] answers) {
 *         Map<Integer, Integer> count = new HashMap<Integer, Integer>();
 *         for (int y : answers) {
 *             count.put(y, count.getOrDefault(y, 0) + 1);
 *         }
 *         int ans = 0;
 *         for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
 *             int y = entry.getKey(), x = entry.getValue();
 *             ans += (x + y) / (y + 1) * (y + 1);
 *         }
 *         return ans;
 *     }
 *
 *
 */